package com.example.androidlearning.sy4;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.androidlearning.R;

import java.util.List;
import java.util.Map;

public class MySimpleAdapter extends SimpleAdapter {
    /**
     * Constructor
     *
     * @param context  The context where the View associated with this SimpleAdapter is running
     * @param data     A List of Maps. Each entry in the List corresponds to one row in the list. The
     *                 Maps contain the data for each row, and should include all the entries specified in
     *                 "from"
     * @param resource Resource identifier of a view layout that defines the views for this list
     *                 item. The layout file should include at least those named views defined in "to"
     * @param from     A list of column names that will be added to the Map associated with each
     *                 item.
     * @param to       The views that should display column in the "from" parameter. These should all be
     *                 TextViews. The first N views in this list are given the values of the first N columns
     */

    Context context;
    List<? extends Map<String, ?>> data;

    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.context = context;
        this.data = data;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View v = super.getView(position, convertView, parent);
        Button btn = v.findViewById(R.id.item_button);
        btn.setTag(position);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showNormalDialog(getData(), position);
            }
        });

        return v;
    }

    private void showNormalDialog(List<? extends Map<String, ?>> data, int i){
        //创建dialog构造器
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(getContent());
        //设置title
        normalDialog.setTitle((CharSequence) data.get(i).get("names"));
        //设置icon
        normalDialog.setIcon(R.mipmap.ic_launcher_round);
        //设置内容
        normalDialog.setMessage((CharSequence) data.get(i).get("names") + "：好好吃");
        //设置按钮
        normalDialog.setPositiveButton("确定"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        //创建并显示
        normalDialog.create().show();
    }

    protected Context getContent(){
        return context;
    }

    protected List<? extends Map<String, ?>> getData(){
        return data;
    }
}
